using System;
using System.Collections.Generic;
using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;

namespace TestDxfDocument
{
    public class ThreadInsert
    {

        private const double Pi = Math.PI;
        /// <summary>
        /// 按照锋宜的螺纹刀片来绘制
        /// </summary>
        /// <param name="data"></param>
        public static void ThreadInsert4Fy(Dictionary<string, object> data)
        {
            var dxf = new DxfDocument();
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);


            double offset = 10;
            double textHeight = 3.5;

            //double scale = 10; //放大系数
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            string path = Convert.ToString(data["path"]);
            try
            {
                path = Convert.ToString(data["path"]);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            #region 变量
            var holeDia = 3.4;//通孔直径
            try
            {
                holeDia = Convert.ToDouble(data["holedia"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[holedia] not found");
            }

            var chamfer4HoleDia = 5.6;//通孔倒角直径
            try
            {
                chamfer4HoleDia = Convert.ToDouble(data["cfdia"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[cfdia] not found");
            }


            var icDia = 9.48;//内切圆直径
            try
            {
                icDia = Convert.ToDouble(data["ic"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[ic] not found");
            }
            var h = 15.4 / 9.48 * icDia;
            var h1 = 4.16 / 9.48 * icDia;
            try
            {
                h1 = Convert.ToDouble(data["lbb"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[lbb] not found");
            }

            var h2 = 1.2 / 9.48 * icDia;
            try
            {
                h2 = Convert.ToDouble(data["pdpt"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdpt] not found");
            }

            var h3 = 1.133 / 9.48 * icDia;
            try
            {
                h3 = Convert.ToDouble(data["pdpt"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdpt] not found");
            }

            var l1 = 1.2 / 9.48 * icDia;
            try
            {
                l1 = Convert.ToDouble(data["pdx"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdx] not found");
            }

            var l2 = 1.2 / 9.48 * icDia;
            try
            {
                l2 = Convert.ToDouble(data["pdy"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdy] not found");
            }

            var cre = 0.25;
            try
            {
                cre = Convert.ToDouble(data["cre"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[cre] not found");
            }

            //齿左侧圆弧
            var retl = 0.25;
            try
            {
                retl = Convert.ToDouble(data["retl"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[retl] not found");
            }

            //齿右侧圆弧
            var retr = 0.25;
            try
            {
                retr = Convert.ToDouble(data["retr"]) * scale;
            }
            catch (Exception e)
            {
                Console.WriteLine("[retr] not found");
            }

            var par = 27.5 * Pi / 180;
            try
            {
                par = Convert.ToDouble(data["par"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[par] not found");
            }

            var pal = 27.5 * Pi / 180;
            try
            {
                pal = Convert.ToDouble(data["pal"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[pal] not found");
            }

            //右侧锥度
            var rightDegree = 0 * Pi / 180;
            //左侧锥度
            var leftDegree = 0 * Pi / 180;
            var r1 = 0.8;

            var alpha1 = 15 * Pi / 180;
            try
            {
                alpha1 = Convert.ToDouble(data["gan"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[gan] not found");
            }

            var alpha2 = 45 * Pi / 180;


            // double S = Convert.ToDouble(data["s"]);
            // double S1 = Convert.ToDouble(data["s1"]);
            double S = 1;
            double S1 = 1;

            double thickness;
            double s;
            if (S == 0)
            {
                //刀片厚度
                thickness = 3.65;
                try
                {
                    thickness = Convert.ToDouble(data["s1"]);
                }
                catch (Exception e)
                {
                    Console.WriteLine("[s1] not found");
                }

                s = thickness + h1 * Math.Tan(alpha1 * 180 / Pi);

            }
            if (S1 == 0)
            {
                //刀尖厚度
                s = 3.55;
                try
                {
                    s = Convert.ToDouble(data["s"]);
                }
                catch (Exception e)
                {
                    Console.WriteLine("[s] not found");
                }

                thickness = s - h1 * Math.Tan(alpha1 * 180 / Pi);

            }
            else
            {
                //刀尖厚度
                s = 3.55;
                try
                {
                    s = Convert.ToDouble(data["s"]);
                }
                catch (Exception e)
                {
                    Console.WriteLine("[s] not found");
                }

                //刀片厚度
                thickness = 3.65;
                try
                {
                    thickness = Convert.ToDouble(data["s1"]);
                }
                catch (Exception e)
                {
                    Console.WriteLine("[s1] not found");
                }
            }

            var chamferCD = 60 * Pi / 180;
            try
            {
                chamferCD = Convert.ToDouble(data["cfagl"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[cfagl] not found");
            }
            #endregion
            String thft = Convert.ToString(data["thft"]);
            /*
            try
            {
                thft = Convert.ToDouble(data["thft"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[thft] not found");
            }
            */


            //string hand = r;
            var hand = "R";
            try
            {
                hand = data["hand"].ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine("[hand] not found");
            }
            var ttp = "EXT";
            try
            {
                ttp = data["ttp"].ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine("[ttp] not found");
            }

            if (hand.Equals("R") && ttp.Equals("EXT") || ttp.Equals("INT") && hand.Equals("L"))
            {
                Console.WriteLine("this is l");
                var origin = new Vector2(0, 0);
                var holeC = new Circle(origin, holeDia / 2);
                holeC.Layer = contour;
                //dxf.AddEntity(holeC);
                fun.drawcircle(dxf, origin, holeDia / 2, 0.3, contour);
                var chamferC = new Circle(origin, chamfer4HoleDia / 2);
                chamferC.Layer = contour;
                //dxf.AddEntity(chamferC);
                fun.drawcircle(dxf, origin, chamfer4HoleDia / 2, 0.3, contour);
                var ic = new Circle(origin, icDia / 2);
                ic.Layer = center;
                dxf.AddEntity(ic);
                //fun.drawcircle(dxf, origin, icDia / 2, 0.3, contour);
                var le = icDia / Math.Tan(Pi / 6);//理论边长（从动），由内切圆尺寸驱动
                var hf = h - le / 2;
                //轮廓点
                var p1 = new Vector2(origin.X + icDia / 2, origin.Y);
                var p3 = new Vector2(p1.X, p1.Y + hf - h2);
                var p2 = new Vector2(p3.X, p3.Y + h2 - h1);


                var cornerRadiusCenter = new Vector2(p3.X - l1, p3.Y + h2 - cre);
                var p6 = Vector2.Polar(cornerRadiusCenter, cre, par);


                var p7 = Vector2.Polar(cornerRadiusCenter, cre, Pi - pal);
                var p7p = Vector2.Polar(p7, 1, -Pi / 2 - pal);
                var p6p = Vector2.Polar(p6, 1, -Pi / 2 + par);
                var p3p = Vector2.Polar(p3, 1, rightDegree);

                var cp = fun.Inters(p6, p6p, p3, p3p);
                var p4 = Vector2.Polar(cp, retr / Math.Tan(Pi / 4 + par / 2 + rightDegree / 2), -rightDegree);
                var p5 = Vector2.Polar(cp, retr / Math.Tan(Pi / 4 + par / 2 + rightDegree / 2), Pi / 2 + par);

                var ll1 = new Vector2(1, cornerRadiusCenter.Y + cre - h3);
                var ll2 = new Vector2(2, cornerRadiusCenter.Y + cre - h3);
                var cl = fun.Inters(ll1, ll2, p7, p7p);

                var p8 = Vector2.Polar(cl, retl / Math.Tan(Pi / 4 + pal / 2 - leftDegree / 2), Pi / 2 - pal);
                var p9 = Vector2.Polar(cl, retl / Math.Tan(Pi / 4 + pal / 2 - leftDegree / 2), Pi - leftDegree);
                var p11 = Vector2.Polar(origin, icDia / 2, 2 * Pi / 3);

                var p11p = Vector2.Polar(p11, 1, Pi / 6);
                var p10 = fun.Inters(p11, p11p, cl, p9);
                var p12 = fun.Inters(p10, p11, p2, Vector2.Polar(p2, 1, Pi));
                var line1 = new Line(p2, p12);
                var line_1 = fun.mirrorline(line1, new Vector2(0, 0), new Vector2(0, 1));
                //line1.Layer = contour;
                //dxf.AddEntity(line1);
                fun.drawBline(dxf, line_1, 0.3, contour);
                //fun.drawBLine(dxf, line1, 0.3, contour);
                //dxf.AddEntity(Pus.RotateByOrigin(Pi * 2 / 3,line1));
                //dxf.AddEntity(Pus.RotateByOrigin(-Pi * 2 / 3, line1));
                fun.drawBline(dxf, Pus.RotateByOrigin(Pi * 2 / 3, line_1), 0.3, contour);
                fun.drawBline(dxf, Pus.RotateByOrigin(-Pi * 2 / 3, line_1), 0.3, contour);
                Vector2 a0 = new Vector2(0, 0);
                Vector2 a1 = fun.Polar(a0, 60 * Pi / 180, icDia / 2 / Math.Cos(60 * Pi / 180));
                Vector2 a2 = fun.Polar(a0, -60 * Pi / 180, icDia / 2 / Math.Cos(60 * Pi / 180));
                Vector2 a3 = fun.Polar(a0, 180 * Pi / 180, icDia / 2 / Math.Cos(60 * Pi / 180));
                LwPolyline line001 = new LwPolyline();
                line001.Vertexes.Add(new LwPolylineVertex(a1));
                line001.Vertexes.Add(new LwPolylineVertex(a2));
                line001.Vertexes.Add(new LwPolylineVertex(a3));
                line001.Vertexes.Add(new LwPolylineVertex(a1));
                var line00_1 = Pus.MirrorPolyline(line001, new Vector2(0, 0), new Vector2(0, 1));
                line00_1.Layer = contour;
                dxf.AddEntity(line00_1);

                LwPolyline contoureLine_1;
                LwPolyline contoureLine;
                if (thft.Equals("2"))
                {
                    Vector2 v7 = fun.Inters(p6, fun.Polar(p6, 180 * Pi / 180, 1), a1, a3);
                    Vector2 v7p = fun.Polar(p6, 180 * Pi / 180, fun.distance1(p6, v7) / 2);
                    Vector2 v7p1 = fun.Polar(v7p, -90 * Pi / 180, 1);
                    Vector2 v8 = fun.mirrorpoint(p5, v7p, v7p1);
                    Vector2 v9 = fun.mirrorpoint(p4, v7p, v7p1);
                    Vector2 v10 = fun.Inters(p10, p11, v9, fun.Polar(v9, 180 * Pi / 180, 1));
                    contoureLine = Pus.AddPointsToLwPolyLine(p1, p2, p3, p4, p5, p6, v7, v8, v9, v10, p11);
                    contoureLine.Layer = contour;
                    contoureLine_1 = Pus.MirrorPolyline(contoureLine, new Vector2(0, 0), new Vector2(0, 1));
                    contoureLine_1.Vertexes[3].Bulge = Math.Tan((Pi / 2 - par - rightDegree) / 4);
                    contoureLine_1.Vertexes[5].Bulge = -0;
                    contoureLine_1.Vertexes[7].Bulge = Math.Tan((leftDegree + Pi / 2 - pal) / 4);
                    fun.PolyFill(dxf, contoureLine_1, 0.3, contour);
                }
                else
                {
                    contoureLine = Pus.AddPointsToLwPolyLine(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
                    contoureLine.Layer = contour;
                    contoureLine_1 = Pus.MirrorPolyline(contoureLine, new Vector2(0, 0), new Vector2(0, 1));
                    contoureLine_1.Vertexes[3].Bulge = Math.Tan((Pi / 2 - par - rightDegree) / 4);
                    contoureLine_1.Vertexes[5].Bulge = -Math.Tan((Pi - pal - par) / 4);
                    contoureLine_1.Vertexes[7].Bulge = Math.Tan((leftDegree + Pi / 2 - pal) / 4);
                    fun.PolyFill(dxf, contoureLine_1, 0.3, contour);
                }

                //旋转
                var contourLine1 = Pus.RotateByOrigin(Pi * 2 / 3, contoureLine_1);
                //dxf.AddEntity(contourLine1);
                fun.PolyFill(dxf, contourLine1, 0.3, contour);
                //再旋转
                var contourLine2 = Pus.RotateByOrigin(-Pi * 2 / 3, contoureLine_1);
                //dxf.AddEntity(contourLine2);
                fun.PolyFill(dxf, contourLine2, 0.3, contour);

                //断屑槽
                var d1 = new Vector2(origin.X + le * 2.5, origin.Y);
                var d2 = new Vector2(d1.X, d1.Y + holeDia / 2);
                var d3 = new Vector2(d2.X, p3.Y);
                var d2p = Pus.KPoint(d2, d3, 0.2);
                var d4 = new Vector2(d1.X, d1.Y + hf);
                var d5 = new Vector2(d4.X - s, d4.Y);
                var d6 = Vector2.Polar(d5, h2 / Math.Cos(alpha1), -Pi / 2 + alpha1);
                var d9 = new Vector2(d1.X - thickness, p12.Y);
                var dc = fun.Inters(d5, d6, d9, Vector2.Polar(d9, 1, Pi / 4));
                var distance = r1 * Math.Tan((alpha1 + alpha2) / 2);
                var d7 = Vector2.Polar(dc, distance, alpha1 + Pi / 2);
                var d8 = Vector2.Polar(dc, distance, -Pi / 2 - alpha2);
                var d10 = new Vector2(d1.X - thickness, d1.Y + chamfer4HoleDia / 2);
                var d11 = Vector2.Polar(d10, (chamfer4HoleDia - holeDia) / (2 * Math.Sin(chamferCD / 2)), -chamferCD / 2);
                var breaker1 = Pus.AddPointsToLwPolyLine(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d2);
                breaker1.Vertexes[6].Bulge = -Math.Tan((alpha1 + alpha2) / 4);
                breaker1.Layer = contour;
                //dxf.AddEntity(breaker1);
                fun.PolyFill(dxf, breaker1, 0.3, contour);
                var line3_6 = new Line(d3, d6);
                line3_6.Layer = contour;
                //dxf.AddEntity(line3_6);
                fun.drawBline(dxf, line3_6, 0.3, contour);
                var d12 = new Vector2(d1.X, d1.Y - holeDia / 2);
                var d13 = new Vector2(d11.X, d12.Y);
                var d14 = new Vector2(d10.X, d10.Y - chamfer4HoleDia);
                var d15 = Vector2.Polar(d14, 1, -Pi / 2);
                var d16 = new Vector2(d12.X, d15.Y);
                var holeLine = Pus.AddPointsToLwPolyLine(d1, d12, d13, d14, d10);
                holeLine.Layer = contour;
                //dxf.AddEntity(holeLine);
                fun.PolyFill(dxf, holeLine, 0.3, contour);
                var line11_13 = new Line(d11, d13);
                line11_13.Layer = contour;
                //dxf.AddEntity(line11_13);
                fun.drawBline(dxf, line11_13, 0.3, contour);

                var line14_15 = new Line(d14, d15);
                line14_15.Layer = contour;
                //dxf.AddEntity(line14_15);
                fun.drawBline(dxf, line14_15, 0.3, contour);

                var line12_16 = new Line(d12, d16);
                line12_16.Layer = contour;
                //dxf.AddEntity(line12_16);
                fun.drawBline(dxf, line12_16, 0.3, contour);

                /*
                var s1 = Pus.TwoPointsSpline(d9, d2p, 7, 3);
                s1.Layer = hatching;
                dxf.AddEntity(s1);*/

                LwPolyline line00 = new LwPolyline();
                line00.Vertexes.Add(new LwPolylineVertex(d9));
                line00.Vertexes.Add(new LwPolylineVertex(new Vector2((d9.X + d2p.X) / 2, (d9.Y + d2p.Y) / 2)));
                line00.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                line00.Vertexes.Add(new LwPolylineVertex(d2p));
                line00.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                line00.Layer = hatching;
                line00.Lineweight = Lineweight.W25;
                dxf.AddEntity(line00);

                LwPolyline poly2 = new LwPolyline();
                poly2.Vertexes.Add(new LwPolylineVertex(d9));
                poly2.Vertexes.Add(new LwPolylineVertex(new Vector2((d9.X + d2p.X) / 2, (d9.Y + d2p.Y) / 2)));
                poly2.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                poly2.Vertexes.Add(new LwPolylineVertex(d2p));
                poly2.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                poly2.Vertexes.Add(new LwPolylineVertex(d2));
                poly2.Vertexes.Add(new LwPolylineVertex(d11));
                poly2.Vertexes.Add(new LwPolylineVertex(d10));
                poly2.Vertexes.Add(new LwPolylineVertex(d9));
                String[] htype1 = new String[100];
                Line[] hline1 = new Line[100];
                Arc[] harc1 = new Arc[100];
                int hnumst1 = poly2.Explode().Count;  //记录爆炸后，线段有多少
                for (int i = 0; i < hnumst1; i++)
                {
                    htype1[i] = poly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype1[i] == "Line")
                    {
                        hline1[i] = (Line)poly2.Explode()[i];
                    }
                    if (htype1[i] == "Arc")
                    {
                        harc1[i] = (Arc)poly2.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype1, hnumst1, 45, 2, hline1, harc1, hatching);




                /*
                var hatch1 = Pus.HatchByPath(new List<EntityObject>() { Pus.AddPointsToLwPolyLine(d2p, d2, d11, d10, d9), s1 }, 45, 3);
                hatch1.Layer = hatching;
                dxf.AddEntity(hatch1);*/




                /*
                var s2 = Pus.TwoPointsSpline(d15, d16, 7, 4);
                s2.Layer = hatching;
                dxf.AddEntity(s2);
                var hatch2 = Pus.HatchByPath(new List<EntityObject>() { Pus.AddPointsToLwPolyLine(d16, d12, d13, d14, d15), s2 }, 45, 3);
                hatch2.Layer = hatching;
                dxf.AddEntity(hatch2);*/

                LwPolyline line01 = new LwPolyline();
                line01.Vertexes.Add(new LwPolylineVertex(d15));
                line01.Vertexes.Add(new LwPolylineVertex(new Vector2((d15.X + d16.X) / 2, (d15.Y + d16.Y) / 2)));
                line01.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                line01.Vertexes.Add(new LwPolylineVertex(d16));
                line01.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                line01.Layer = hatching;
                line01.Lineweight = Lineweight.W25;
                dxf.AddEntity(line01);

                LwPolyline poly3 = new LwPolyline();
                poly3.Vertexes.Add(new LwPolylineVertex(d15));
                poly3.Vertexes.Add(new LwPolylineVertex(new Vector2((d15.X + d16.X) / 2, (d15.Y + d16.Y) / 2)));
                poly3.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                poly3.Vertexes.Add(new LwPolylineVertex(d16));
                poly3.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                poly3.Vertexes.Add(new LwPolylineVertex(d12));
                poly3.Vertexes.Add(new LwPolylineVertex(d13));
                poly3.Vertexes.Add(new LwPolylineVertex(d14));
                poly3.Vertexes.Add(new LwPolylineVertex(d15));
                String[] htype2 = new String[100];
                Line[] hline2 = new Line[100];
                Arc[] harc2 = new Arc[100];
                int hnumst2 = poly3.Explode().Count;  //记录爆炸后，线段有多少
                for (int i = 0; i < hnumst2; i++)
                {
                    htype2[i] = poly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype2[i] == "Line")
                    {
                        hline2[i] = (Line)poly3.Explode()[i];
                    }
                    if (htype2[i] == "Arc")
                    {
                        harc2[i] = (Arc)poly3.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype2, hnumst2, 45, 2, hline2, harc2, hatching);



                //局部放大图
                var scaling = 4.0;
                var sm2 = Matrix2.Identity * scaling;//缩放vector2的矩阵
                var scalLine = Pus.ScalingByMatrix2(contoureLine_1, sm2);
                //var scalLine = Pus.OffsetLwPolyline(scalLine0, new Vector2(10, 0));
                //dxf.AddEntity(scalLine);
                fun.PolyFill(dxf, scalLine, 0.3, contour);

                //标注
                DimensionStyle dimensionStyle = fun.DimStyle(true);
                DimensionStyle dimensionStyle2 = fun.DimStyle(true);


                var dim_ic = new DiametricDimension(ic, -45, offset, dimensionStyle);
                dim_ic.UserText = "IC = " + Math.Round(icDia, 4);
                dxf.AddEntity(dim_ic);




                Vector2 arc2Cen;
                if (thft.Equals("2"))
                {
                    //arc2Cen = Pus.ToV2(arc2.Center);
                    Vector2 c1 = scalLine.Vertexes[5].Position;
                    Vector2 c2 = scalLine.Vertexes[6].Position;

                    arc2Cen = new Vector2((c1.X + c2.X) / 2, (c1.Y + c2.Y) / 2);

                    var dim_h2 = new LinearDimension(new Line(sm2 * p3, new Vector2(arc2Cen.X, arc2Cen.Y)), offset * 4, -90, dimensionStyle);
                    dim_h2.UserText = Math.Round(h2, 4) + "";
                    // dxf.AddEntity(dim_h2);

                    var arc1Dim = Pus.ReDimension(scalLine, 1, dimensionStyle, 45, offset);
                    arc1Dim.UserText = "R";// + Math.Round(retr,4);
                    dxf.AddEntity(arc1Dim);

                    var arc2Dim = Pus.ReDimension(scalLine, 2, dimensionStyle, 90, offset);
                    arc2Dim.UserText = "R";// + Math.Round(r,4);
                                           //dxf.AddEntity(arc2Dim);

                    var arc3Dim = Pus.ReDimension(scalLine, 2, dimensionStyle, 130, offset);
                    arc3Dim.UserText = "R";// + Math.Round(retl,4);
                    dxf.AddEntity(arc3Dim);
                }
                else
                {
                    var arc2 = Pus.GetArcFromPline(scalLine, 2);
                    arc2Cen = Pus.ToV2(arc2.Center);

                    var dim_h2 = new LinearDimension(new Line(sm2 * p3, new Vector2(arc2Cen.X, arc2Cen.Y + cre * scaling)), offset * 4, -90, dimensionStyle);
                    dim_h2.UserText = Math.Round(h2, 4) + "";
                    // dxf.AddEntity(dim_h2);

                    var arc1Dim = Pus.ReDimension(scalLine, 1, dimensionStyle, 45, offset);
                    arc1Dim.UserText = "R";// + Math.Round(retr,4);
                    dxf.AddEntity(arc1Dim);

                    var arc2Dim = Pus.ReDimension(scalLine, 2, dimensionStyle, 90, offset);
                    arc2Dim.UserText = "R";// + Math.Round(r,4);
                    dxf.AddEntity(arc2Dim);

                    var arc3Dim = Pus.ReDimension(scalLine, 3, dimensionStyle, 130, offset);
                    arc3Dim.UserText = "R";// + Math.Round(retl,4);
                    dxf.AddEntity(arc3Dim);
                }

                var arc2CenP = new Vector2(arc2Cen.X, p3.Y);
                var cenLine = new Line(fun.Polar(arc2Cen, 90 * Pi / 180, 5), arc2CenP);
                cenLine.Layer = center;
                dxf.AddEntity(cenLine);
                /*
                var dim_l1 = new LinearDimension(new Line(sm2 * p3, arc2Cen), - offset * 6, 0, dimensionStyle);
                dim_l1.UserText = Math.Round(l1, 4) + "";
                dxf.AddEntity(dim_l1);*/



                var dim_Par =
                    new Angular2LineDimension(cenLine, new Line(scalLine.Vertexes[5].Position, scalLine.Vertexes[4].Position), offset * 4, dimensionStyle);
                dim_Par.UserText = "PAR=" + Math.Round(par * 180 / Pi, 4) + "°";
                dxf.AddEntity(dim_Par);

                var dim_Pal =
                    new Angular2LineDimension(cenLine, new Line(scalLine.Vertexes[6].Position, scalLine.Vertexes[7].Position), offset * 4, dimensionStyle);
                dim_Pal.UserText = "PAL=" + Math.Round(pal * 180 / Pi, 4) + "°";
                dxf.AddEntity(dim_Pal);

                //刀片厚度
                var dim_thickness = new LinearDimension(new Line(d15, d16), offset, 180, dimensionStyle2);
                dim_thickness.UserText = "S1=" + Math.Round(thickness, 4) + "";
                dxf.AddEntity(dim_thickness);

                //刀尖厚度
                var dim_s = new LinearDimension(new Line(d4, d5), offset, 0, dimensionStyle2);
                dim_s.UserText = "S=" + Math.Round(s, 4) + "";
                dxf.AddEntity(dim_s);

                //前角
                var dim_alpha1 =
                    new Angular2LineDimension(new Line(d5, Vector2.Polar(d5, 1, Pi / 2)), new Line(d5, Vector2.Polar(d5, 1, Vector2.Angle(d6, d5))), offset * 4, dimensionStyle);
                dim_alpha1.UserText = "GAN=" + Math.Round(alpha1 * 180 / Pi, 4) + "°";
                dxf.AddEntity(dim_alpha1);
                //Pus.NeedleDim(dxf, new Line(d5, Vector2.Polar(d5, 1, Pi / 2)), new Line(d5, Vector2.Polar(d5, 1, Vector2.Angle(d6, d5))), offset * 4, Math.Round(alpha1 * 180 / Pi, 4) + "°", dimensionStyle);

                //孔直径
                var dim_holedia = new LinearDimension(new Line(d2, d12), offset + (d11.X - d2.X), -90, dimensionStyle);
                dim_holedia.UserText = "HOLEDIA=" + Math.Round(holeDia, 4) + "";
                dxf.AddEntity(dim_holedia);

                //段屑槽长度
                var dim_breaker = new LinearDimension(new Line(d5, d9), offset, 90, dimensionStyle);
                dim_breaker.UserText = "LBB=" + Math.Round(h1, 3) + "";
                dxf.AddEntity(dim_breaker);

                //沉头孔角度
                /*
                var dim_chamfer =
                    new Angular2LineDimension(new Line(d11, d10), new Line(d13, d14), offset * 4.5, dimensionStyle);
                dim_chamfer.UserText = Math.Round(chamferCD * 180 / Pi, 2) + "°";
                dxf.AddEntity(dim_chamfer);*/

                fun.jdbz1(dxf, new Line(d11, d10), new Line(d13, d14), offset * 3 + d10.Y / Math.Tan(chamferCD / 2), Math.Round(chamferCD * 180 / Pi, 2) + "°", 1.5);

                //沉头孔最大直径
                var dim_chamferDia = new LinearDimension(new Line(d10, d14), offset, 90, dimensionStyle);
                dim_chamferDia.UserText = "CFDIA=" + Math.Round(chamfer4HoleDia, 3) + "";
                dxf.AddEntity(dim_chamferDia);

                if (thft.Equals("2"))
                {
                    //arc2Cen1 = Pus.ToV2(arc2.Center);
                    Vector2 c1 = contoureLine.Vertexes[5].Position;
                    Vector2 c2 = contoureLine.Vertexes[6].Position;
                    Vector2 arc2Cen1 = new Vector2((c1.X + c2.X) / 2, (c1.Y + c2.Y) / 2);

                    //PDX
                    LinearDimension pdx = new LinearDimension(new Line(c2, a1), offset + Math.Abs(c2.Y - a1.Y) / 2, 0, dimensionStyle);
                    pdx.UserText = String.Format("PDX={0}", l1);
                    dxf.AddEntity(pdx);
                    //PDY
                    LinearDimension pdy = new LinearDimension(new Line(c2, a1), offset + Math.Abs(c2.X - a1.X) / 2, -90, dimensionStyle2);
                    pdy.UserText = String.Format("PDY={0}", l2);
                    dxf.AddEntity(pdy);
                }
                else
                {
                    var arc2 = Pus.GetArcFromPline(contoureLine, 2);
                    Vector2 arc2Cen1 = Pus.ToV2(arc2.Center);

                    //PDX
                    LinearDimension pdx = new LinearDimension(new Line(arc2Cen1, a1), offset + Math.Abs(arc2Cen1.Y - a1.Y) / 2, 0, dimensionStyle);
                    pdx.UserText = String.Format("PDX={0}", l1);
                    dxf.AddEntity(pdx);
                    //PDY
                    LinearDimension pdy = new LinearDimension(new Line(arc2Cen1, a1), offset + Math.Abs(arc2Cen1.X - a1.X) / 2, -90, dimensionStyle2);
                    pdy.UserText = String.Format("PDY={0}", l2);
                    dxf.AddEntity(pdy);
                }
            }
            else
            {
                Console.WriteLine("this is l");
                var origin = new Vector2(0, 0);
                var holeC = new Circle(origin, holeDia / 2);
                holeC.Layer = contour;
                //dxf.AddEntity(holeC);
                fun.drawcircle(dxf, origin, holeDia / 2, 0.3, contour);
                var chamferC = new Circle(origin, chamfer4HoleDia / 2);
                chamferC.Layer = contour;
                //dxf.AddEntity(chamferC);
                fun.drawcircle(dxf, origin, chamfer4HoleDia / 2, 0.3, contour);
                var ic = new Circle(origin, icDia / 2);
                ic.Layer = center;
                dxf.AddEntity(ic);
                //fun.drawcircle(dxf, origin, icDia / 2, 0.3, contour);
                var le = icDia / Math.Tan(Pi / 6);//理论边长（从动），由内切圆尺寸驱动
                var hf = h - le / 2;
                //轮廓点
                var p1 = new Vector2(origin.X + icDia / 2, origin.Y);
                var p3 = new Vector2(p1.X, p1.Y + hf - h2);
                var p2 = new Vector2(p3.X, p3.Y + h2 - h1);


                var cornerRadiusCenter = new Vector2(p3.X - l1, p3.Y + h2 - cre);
                var p6 = Vector2.Polar(cornerRadiusCenter, cre, par);
                var p7 = Vector2.Polar(cornerRadiusCenter, cre, Pi - pal);
                var p7p = Vector2.Polar(p7, 1, -Pi / 2 - pal);
                var p6p = Vector2.Polar(p6, 1, -Pi / 2 + par);
                var p3p = Vector2.Polar(p3, 1, rightDegree);

                var cp = fun.Inters(p6, p6p, p3, p3p);
                var p4 = Vector2.Polar(cp, retr / Math.Tan(Pi / 4 + par / 2 + rightDegree / 2), -rightDegree);
                var p5 = Vector2.Polar(cp, retr / Math.Tan(Pi / 4 + par / 2 + rightDegree / 2), Pi / 2 + par);

                var ll1 = new Vector2(1, cornerRadiusCenter.Y + cre - h3);
                var ll2 = new Vector2(2, cornerRadiusCenter.Y + cre - h3);
                var cl = fun.Inters(ll1, ll2, p7, p7p);

                var p8 = Vector2.Polar(cl, retl / Math.Tan(Pi / 4 + pal / 2 - leftDegree / 2), Pi / 2 - pal);
                var p9 = Vector2.Polar(cl, retl / Math.Tan(Pi / 4 + pal / 2 - leftDegree / 2), Pi - leftDegree);
                var p11 = Vector2.Polar(origin, icDia / 2, 2 * Pi / 3);

                var p11p = Vector2.Polar(p11, 1, Pi / 6);
                var p10 = fun.Inters(p11, p11p, cl, p9);
                var p12 = fun.Inters(p10, p11, p2, Vector2.Polar(p2, 1, Pi));
                var line1 = new Line(p2, p12);
                //line1.Layer = contour;
                //dxf.AddEntity(line1);
                fun.drawBline(dxf, line1, 0.3, contour);
                //fun.drawBLine(dxf, line1, 0.3, contour);
                //dxf.AddEntity(Pus.RotateByOrigin(Pi * 2 / 3,line1));
                //dxf.AddEntity(Pus.RotateByOrigin(-Pi * 2 / 3, line1));
                fun.drawBline(dxf, Pus.RotateByOrigin(Pi * 2 / 3, line1), 0.3, contour);
                fun.drawBline(dxf, Pus.RotateByOrigin(-Pi * 2 / 3, line1), 0.3, contour);
                Vector2 a0 = new Vector2(0, 0);
                Vector2 a1 = fun.Polar(a0, 60 * Pi / 180, icDia / 2 / Math.Cos(60 * Pi / 180));
                Vector2 a2 = fun.Polar(a0, -60 * Pi / 180, icDia / 2 / Math.Cos(60 * Pi / 180));
                Vector2 a3 = fun.Polar(a0, 180 * Pi / 180, icDia / 2 / Math.Cos(60 * Pi / 180));
                LwPolyline line001 = new LwPolyline();
                line001.Vertexes.Add(new LwPolylineVertex(a1));
                line001.Vertexes.Add(new LwPolylineVertex(a2));
                line001.Vertexes.Add(new LwPolylineVertex(a3));
                line001.Vertexes.Add(new LwPolylineVertex(a1));
                line001.Layer = contour;
                dxf.AddEntity(line001);

                LwPolyline contoureLine;
                if (thft.Equals("2"))
                {
                    Vector2 v7 = fun.Inters(p6, fun.Polar(p6, 180 * Pi / 180, 1), a1, a3);
                    Vector2 v7p = fun.Polar(p6, 180 * Pi / 180, fun.distance1(p6, v7) / 2);
                    Vector2 v7p1 = fun.Polar(v7p, -90 * Pi / 180, 1);
                    Vector2 v8 = fun.mirrorpoint(p5, v7p, v7p1);
                    Vector2 v9 = fun.mirrorpoint(p4, v7p, v7p1);
                    Vector2 v10 = fun.Inters(p10, p11, v9, fun.Polar(v9, 180 * Pi / 180, 1));
                    contoureLine = Pus.AddPointsToLwPolyLine(p1, p2, p3, p4, p5, p6, v7, v8, v9, v10, p11);
                    contoureLine.Layer = contour;
                    contoureLine.Vertexes[3].Bulge = -Math.Tan((Pi / 2 - par - rightDegree) / 4);
                    contoureLine.Vertexes[5].Bulge = 0;
                    contoureLine.Vertexes[7].Bulge = -Math.Tan((leftDegree + Pi / 2 - pal) / 4);
                    fun.PolyFill(dxf, contoureLine, 0.3, contour);
                }
                else
                {
                    contoureLine = Pus.AddPointsToLwPolyLine(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
                    contoureLine.Layer = contour;
                    contoureLine.Vertexes[3].Bulge = -Math.Tan((Pi / 2 - par - rightDegree) / 4);
                    contoureLine.Vertexes[5].Bulge = Math.Tan((Pi - pal - par) / 4);
                    contoureLine.Vertexes[7].Bulge = -Math.Tan((leftDegree + Pi / 2 - pal) / 4);
                    fun.PolyFill(dxf, contoureLine, 0.3, contour);
                }

                //旋转
                var contourLine1 = Pus.RotateByOrigin(Pi * 2 / 3, contoureLine);
                //dxf.AddEntity(contourLine1);
                fun.PolyFill(dxf, contourLine1, 0.3, contour);
                //再旋转
                var contourLine2 = Pus.RotateByOrigin(-Pi * 2 / 3, contoureLine);
                //dxf.AddEntity(contourLine2);
                fun.PolyFill(dxf, contourLine2, 0.3, contour);

                //断屑槽
                var d1 = new Vector2(origin.X - le * 1.5, origin.Y);
                var d2 = new Vector2(d1.X, d1.Y + holeDia / 2);
                var d3 = new Vector2(d2.X, p3.Y);
                var d2p = Pus.KPoint(d2, d3, 0.2);
                var d4 = new Vector2(d1.X, d1.Y + hf);
                var d5 = new Vector2(d4.X - s, d4.Y);
                var d6 = Vector2.Polar(d5, h2 / Math.Cos(alpha1), -Pi / 2 + alpha1);
                var d9 = new Vector2(d1.X - thickness, p12.Y);
                var dc = fun.Inters(d5, d6, d9, Vector2.Polar(d9, 1, Pi / 4));
                var distance = r1 * Math.Tan((alpha1 + alpha2) / 2);
                var d7 = Vector2.Polar(dc, distance, alpha1 + Pi / 2);
                var d8 = Vector2.Polar(dc, distance, -Pi / 2 - alpha2);
                var d10 = new Vector2(d1.X - thickness, d1.Y + chamfer4HoleDia / 2);
                var d11 = Vector2.Polar(d10, (chamfer4HoleDia - holeDia) / (2 * Math.Sin(chamferCD / 2)), -chamferCD / 2);
                var breaker1 = Pus.AddPointsToLwPolyLine(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d2);
                breaker1.Vertexes[6].Bulge = -Math.Tan((alpha1 + alpha2) / 4);
                breaker1.Layer = contour;
                //dxf.AddEntity(breaker1);
                fun.PolyFill(dxf, breaker1, 0.3, contour);
                var line3_6 = new Line(d3, d6);
                line3_6.Layer = contour;
                //dxf.AddEntity(line3_6);
                fun.drawBline(dxf, line3_6, 0.3, contour);
                var d12 = new Vector2(d1.X, d1.Y - holeDia / 2);
                var d13 = new Vector2(d11.X, d12.Y);
                var d14 = new Vector2(d10.X, d10.Y - chamfer4HoleDia);
                var d15 = Vector2.Polar(d14, 1, -Pi / 2);
                var d16 = new Vector2(d12.X, d15.Y);
                var holeLine = Pus.AddPointsToLwPolyLine(d1, d12, d13, d14, d10);
                holeLine.Layer = contour;
                //dxf.AddEntity(holeLine);
                fun.PolyFill(dxf, holeLine, 0.3, contour);
                var line11_13 = new Line(d11, d13);
                line11_13.Layer = contour;
                //dxf.AddEntity(line11_13);
                fun.drawBline(dxf, line11_13, 0.3, contour);

                var line14_15 = new Line(d14, d15);
                line14_15.Layer = contour;
                //dxf.AddEntity(line14_15);
                fun.drawBline(dxf, line14_15, 0.3, contour);

                var line12_16 = new Line(d12, d16);
                line12_16.Layer = contour;
                //dxf.AddEntity(line12_16);
                fun.drawBline(dxf, line12_16, 0.3, contour);

                /*
                var s1 = Pus.TwoPointsSpline(d9, d2p, 7, 3);
                s1.Layer = hatching;
                dxf.AddEntity(s1);*/

                LwPolyline line00 = new LwPolyline();
                line00.Vertexes.Add(new LwPolylineVertex(d9));
                line00.Vertexes.Add(new LwPolylineVertex(new Vector2((d9.X + d2p.X) / 2, (d9.Y + d2p.Y) / 2)));
                line00.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                line00.Vertexes.Add(new LwPolylineVertex(d2p));
                line00.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                line00.Layer = hatching;
                line00.Lineweight = Lineweight.W25;
                dxf.AddEntity(line00);

                LwPolyline poly2 = new LwPolyline();
                poly2.Vertexes.Add(new LwPolylineVertex(d9));
                poly2.Vertexes.Add(new LwPolylineVertex(new Vector2((d9.X + d2p.X) / 2, (d9.Y + d2p.Y) / 2)));
                poly2.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                poly2.Vertexes.Add(new LwPolylineVertex(d2p));
                poly2.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                poly2.Vertexes.Add(new LwPolylineVertex(d2));
                poly2.Vertexes.Add(new LwPolylineVertex(d11));
                poly2.Vertexes.Add(new LwPolylineVertex(d10));
                poly2.Vertexes.Add(new LwPolylineVertex(d9));
                String[] htype1 = new String[100];
                Line[] hline1 = new Line[100];
                Arc[] harc1 = new Arc[100];
                int hnumst1 = poly2.Explode().Count;  //记录爆炸后，线段有多少
                for (int i = 0; i < hnumst1; i++)
                {
                    htype1[i] = poly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype1[i] == "Line")
                    {
                        hline1[i] = (Line)poly2.Explode()[i];
                    }
                    if (htype1[i] == "Arc")
                    {
                        harc1[i] = (Arc)poly2.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype1, hnumst1, 45, 2, hline1, harc1, hatching);




                /*
                var hatch1 = Pus.HatchByPath(new List<EntityObject>() { Pus.AddPointsToLwPolyLine(d2p, d2, d11, d10, d9), s1 }, 45, 3);
                hatch1.Layer = hatching;
                dxf.AddEntity(hatch1);*/




                /*
                var s2 = Pus.TwoPointsSpline(d15, d16, 7, 4);
                s2.Layer = hatching;
                dxf.AddEntity(s2);
                var hatch2 = Pus.HatchByPath(new List<EntityObject>() { Pus.AddPointsToLwPolyLine(d16, d12, d13, d14, d15), s2 }, 45, 3);
                hatch2.Layer = hatching;
                dxf.AddEntity(hatch2);*/

                LwPolyline line01 = new LwPolyline();
                line01.Vertexes.Add(new LwPolylineVertex(d15));
                line01.Vertexes.Add(new LwPolylineVertex(new Vector2((d15.X + d16.X) / 2, (d15.Y + d16.Y) / 2)));
                line01.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                line01.Vertexes.Add(new LwPolylineVertex(d16));
                line01.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                line01.Layer = hatching;
                line01.Lineweight = Lineweight.W25;
                dxf.AddEntity(line01);

                LwPolyline poly3 = new LwPolyline();
                poly3.Vertexes.Add(new LwPolylineVertex(d15));
                poly3.Vertexes.Add(new LwPolylineVertex(new Vector2((d15.X + d16.X) / 2, (d15.Y + d16.Y) / 2)));
                poly3.Vertexes[0].Bulge = Math.Tan(30 * Pi / 180 / 4);
                poly3.Vertexes.Add(new LwPolylineVertex(d16));
                poly3.Vertexes[1].Bulge = Math.Tan(-30 * Pi / 180 / 4);
                poly3.Vertexes.Add(new LwPolylineVertex(d12));
                poly3.Vertexes.Add(new LwPolylineVertex(d13));
                poly3.Vertexes.Add(new LwPolylineVertex(d14));
                poly3.Vertexes.Add(new LwPolylineVertex(d15));
                String[] htype2 = new String[100];
                Line[] hline2 = new Line[100];
                Arc[] harc2 = new Arc[100];
                int hnumst2 = poly3.Explode().Count;  //记录爆炸后，线段有多少
                for (int i = 0; i < hnumst2; i++)
                {
                    htype2[i] = poly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype2[i] == "Line")
                    {
                        hline2[i] = (Line)poly3.Explode()[i];
                    }
                    if (htype2[i] == "Arc")
                    {
                        harc2[i] = (Arc)poly3.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype2, hnumst2, 45, 2, hline2, harc2, hatching);



                //局部放大图
                var scaling = 4.0;
                var sm2 = Matrix2.Identity * scaling;//缩放vector2的矩阵
                var scalLine = Pus.ScalingByMatrix2(contoureLine, sm2);
                //var scalLine = Pus.OffsetLwPolyline(scalLine0, new Vector2(10, 0));
                //dxf.AddEntity(scalLine);
                fun.PolyFill(dxf, scalLine, 0.3, contour);

                //标注
                DimensionStyle dimensionStyle = fun.DimStyle(true);
                DimensionStyle dimensionStyle2 = fun.DimStyle(false);


                var dim_ic = new DiametricDimension(ic, -45, offset, dimensionStyle);
                dim_ic.UserText = "IC = " + Math.Round(icDia, 4);
                dxf.AddEntity(dim_ic);




                Vector2 arc2Cen;
                if (thft.Equals("2"))
                {
                    //arc2Cen = Pus.ToV2(arc2.Center);
                    Vector2 c1 = scalLine.Vertexes[5].Position;
                    Vector2 c2 = scalLine.Vertexes[6].Position;

                    arc2Cen = new Vector2((c1.X + c2.X) / 2, (c1.Y + c2.Y) / 2);

                    var dim_h2 = new LinearDimension(new Line(sm2 * p3, new Vector2(arc2Cen.X, arc2Cen.Y)), offset * 4, -90, dimensionStyle);
                    dim_h2.UserText = Math.Round(h2, 4) + "";
                    // dxf.AddEntity(dim_h2);

                    var arc1Dim = Pus.ReDimension(scalLine, 1, dimensionStyle, 45, offset);
                    arc1Dim.UserText = "R";// + Math.Round(retr,4);
                    dxf.AddEntity(arc1Dim);

                    var arc2Dim = Pus.ReDimension(scalLine, 2, dimensionStyle, 90, offset);
                    arc2Dim.UserText = "R";// + Math.Round(r,4);
                                           //dxf.AddEntity(arc2Dim);

                    var arc3Dim = Pus.ReDimension(scalLine, 2, dimensionStyle, 130, offset);
                    arc3Dim.UserText = "R";// + Math.Round(retl,4);
                    dxf.AddEntity(arc3Dim);
                }
                else
                {
                    var arc2 = Pus.GetArcFromPline(scalLine, 2);
                    arc2Cen = Pus.ToV2(arc2.Center);

                    var dim_h2 = new LinearDimension(new Line(sm2 * p3, new Vector2(arc2Cen.X, arc2Cen.Y + cre * scaling)), offset * 4, -90, dimensionStyle);
                    dim_h2.UserText = Math.Round(h2, 4) + "";
                    // dxf.AddEntity(dim_h2);

                    var arc1Dim = Pus.ReDimension(scalLine, 1, dimensionStyle, 45, offset);
                    arc1Dim.UserText = "R";// + Math.Round(retr,4);
                    dxf.AddEntity(arc1Dim);

                    var arc2Dim = Pus.ReDimension(scalLine, 2, dimensionStyle, 90, offset);
                    arc2Dim.UserText = "R";// + Math.Round(r,4);
                    dxf.AddEntity(arc2Dim);

                    var arc3Dim = Pus.ReDimension(scalLine, 3, dimensionStyle, 130, offset);
                    arc3Dim.UserText = "R";// + Math.Round(retl,4);
                    dxf.AddEntity(arc3Dim);
                }

                var arc2CenP = new Vector2(arc2Cen.X, p3.Y);
                var cenLine = new Line(fun.Polar(arc2Cen, 90 * Pi / 180, 5), arc2CenP);
                cenLine.Layer = center;
                dxf.AddEntity(cenLine);
                /*
                var dim_l1 = new LinearDimension(new Line(sm2 * p3, arc2Cen), - offset * 6, 0, dimensionStyle);
                dim_l1.UserText = Math.Round(l1, 4) + "";
                dxf.AddEntity(dim_l1);*/



                var dim_Par =
                    new Angular2LineDimension(cenLine, new Line(scalLine.Vertexes[5].Position, scalLine.Vertexes[4].Position), offset * 4, dimensionStyle);
                dim_Par.UserText = "PAR=" + Math.Round(par * 180 / Pi, 4) + "°";
                dxf.AddEntity(dim_Par);

                var dim_Pal =
                    new Angular2LineDimension(cenLine, new Line(scalLine.Vertexes[6].Position, scalLine.Vertexes[7].Position), offset * 4, dimensionStyle);
                dim_Pal.UserText = "PAL=" + Math.Round(pal * 180 / Pi, 4) + "°";
                dxf.AddEntity(dim_Pal);

                //刀片厚度
                var dim_thickness = new LinearDimension(new Line(d15, d16), offset, 180, dimensionStyle2);
                dim_thickness.UserText = "S1=" + Math.Round(thickness, 4) + "";
                dxf.AddEntity(dim_thickness);

                //刀尖厚度
                var dim_s = new LinearDimension(new Line(d4, d5), offset, 0, dimensionStyle2);
                dim_s.UserText = "S=" + Math.Round(s, 4) + "";
                dxf.AddEntity(dim_s);

                //前角
                var dim_alpha1 =
                    new Angular2LineDimension(new Line(d5, Vector2.Polar(d5, 1, Pi / 2)), new Line(d5, Vector2.Polar(d5, 1, Vector2.Angle(d6, d5))), offset * 4, dimensionStyle);
                dim_alpha1.UserText = "GAN=" + Math.Round(alpha1 * 180 / Pi, 4) + "°";
                dxf.AddEntity(dim_alpha1);
                //Pus.NeedleDim(dxf, new Line(d5, Vector2.Polar(d5, 1, Pi / 2)), new Line(d5, Vector2.Polar(d5, 1, Vector2.Angle(d6, d5))), offset * 4, Math.Round(alpha1 * 180 / Pi, 4) + "°", dimensionStyle);

                //孔直径
                var dim_holedia = new LinearDimension(new Line(d2, d12), offset+(d11.X-d2.X) , -90, dimensionStyle);
                dim_holedia.UserText = "HOLEDIA=" + Math.Round(holeDia, 4) + "";
                dxf.AddEntity(dim_holedia);

                //段屑槽长度
                var dim_breaker = new LinearDimension(new Line(d5, d9), offset, 90, dimensionStyle);
                dim_breaker.UserText = "LBB=" + Math.Round(h1, 3) + "";
                dxf.AddEntity(dim_breaker);

                //沉头孔角度
                /*
                var dim_chamfer =
                    new Angular2LineDimension(new Line(d11, d10), new Line(d13, d14), offset * 4.5, dimensionStyle);
                dim_chamfer.UserText = Math.Round(chamferCD * 180 / Pi, 2) + "°";
                dxf.AddEntity(dim_chamfer);*/

                fun.jdbz1(dxf, new Line(d11, d10), new Line(d13, d14), offset * 3+d10.Y/Math .Tan(chamferCD/2), Math.Round(chamferCD * 180 / Pi, 2) + "°", 1.5);

                //沉头孔最大直径
                var dim_chamferDia = new LinearDimension(new Line(d10, d14), offset, 90, dimensionStyle);
                dim_chamferDia.UserText = "CFDIA=" + Math.Round(chamfer4HoleDia, 3) + "";
                dxf.AddEntity(dim_chamferDia);

                if (thft.Equals("2"))
                {
                    //arc2Cen1 = Pus.ToV2(arc2.Center);
                    Vector2 c1 = contoureLine.Vertexes[5].Position;
                    Vector2 c2 = contoureLine.Vertexes[6].Position;
                    Vector2 arc2Cen1 = new Vector2((c1.X + c2.X) / 2, (c1.Y + c2.Y) / 2);

                    //PDX
                    LinearDimension pdx = new LinearDimension(new Line(c2, a1), offset+Math.Abs (c2.Y-a1.Y)/2 , 0, dimensionStyle);
                    pdx.UserText = String.Format("PDX={0}", l1);
                    dxf.AddEntity(pdx);
                    //PDY
                    LinearDimension pdy = new LinearDimension(new Line(c2, a1), offset +Math .Abs (c2.X-a1.X)/2, -90, dimensionStyle2);
                    pdy.UserText = String.Format("PDY={0}", l2);
                    dxf.AddEntity(pdy);
                }
                else
                {
                    var arc2 = Pus.GetArcFromPline(contoureLine, 2);
                    Vector2 arc2Cen1 = Pus.ToV2(arc2.Center);

                    //PDX
                    LinearDimension pdx = new LinearDimension(new Line(arc2Cen1, a1), offset + Math.Abs(arc2Cen1.Y - a1.Y)/2, 0, dimensionStyle);
                    pdx.UserText = String.Format("PDX={0}", l1);
                    dxf.AddEntity(pdx);
                    //PDY
                    LinearDimension pdy = new LinearDimension(new Line(arc2Cen1, a1), offset + Math.Abs(arc2Cen1.X - a1.X)/2, -90, dimensionStyle2);
                    pdy.UserText = String.Format("PDY={0}", l2);
                    dxf.AddEntity(pdy);
                }

            }

            /*
            switch (hand)
            {
                case "r":
                    Console.WriteLine("this is r");



                    break;
                    
                case "l":
                   

                    break;
                default:
                    Console.WriteLine("this is n");
                    break;
            }
            */

            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - offset * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
            Console.WriteLine("生成DXF文件" + path);
        }

        public static void DetailInsert()
        {
            var dxf = new DxfDocument();
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);

            var path = @"D:\PPPPPPPPPPPPPPPPP\dxf\螺纹刀片\DetailInsert.dxf";

            //圆
            var angle0 = 144 * Pi / 180;
            var dis0 = 0.003612;
            var origin = Vector2.Polar(Vector2.Zero, dis0, angle0);
            var r0 = 2.399491;
            var circle = new Circle(origin, r0);
            circle.Layer = contour;
            dxf.AddEntity(circle);
            //每个点的极坐标由origin作为基点，不是上一个点，从而减小误差
            var angle1 = 115 * Pi / 180;
            var dis1 = 7.484439;
            var p1 = Vector2.Polar(origin, dis1, angle1);
            var angle2 = 115.4477 * Pi / 180;
            var dis2 = 7.819216;
            var p2 = Vector2.Polar(origin, dis2, angle2);
            var line1_2 = new Line(p1, p2);
            line1_2.Layer = contour;
            dxf.AddEntity(line1_2);

            var angle3 = 115.664047 * Pi / 180;
            var dis3 = 8.152572;
            var p3 = Vector2.Polar(origin, dis3, angle3);
            var line2_3 = new Line(p2, p3);
            line2_3.Layer = contour;
            dxf.AddEntity(line2_3);

            var angle4 = 115.692846 * Pi / 180;
            var dis4 = 8.152210;
            var p4 = Vector2.Polar(origin, dis4, angle4);
            var line3_4 = new Line(p3, p4);
            line3_4.Layer = contour;
            dxf.AddEntity(line3_4);

            //第一段弧终点
            var angle5 = 117.738441 * Pi / 180;
            var dis5 = 8.306126;
            var p5 = Vector2.Polar(origin, dis5, angle5);
            var arc1R = 0.194808;
            var arc1D = 116.690547 * Pi / 180;
            var arc1 = Pus.TwoPointArc(p4, p5, arc1R, true);
            arc1.Layer = contour;
            dxf.AddEntity(arc1);

            //第二段弧的起点
            var angle6 = 117.735878 * Pi / 180;
            var dis6 = 8.305464;
            var p6 = Vector2.Polar(origin, dis6, angle6);
            var line5_6 = new Line(p5, p6);
            line5_6.Layer = contour;
            dxf.AddEntity(line5_6);
            //第二段弧终点
            var angle7 = 118.000011 * Pi / 180;
            var dis7 = 8.284206;
            var p7 = Vector2.Polar(origin, dis7, angle7);
            var arc2R = 0.191274;
            var arc2D = 13.134095 * Pi / 180;
            var arc2 = Pus.TwoPointArc(p6, p7, arc2R, true);
            arc2.Layer = contour;
            dxf.AddEntity(arc2);

            var angle8 = 120.660631 * Pi / 180;
            var dis8 = 8.064913;
            var p8 = Vector2.Polar(origin, dis8, angle8);
            var line7_8 = new Line(p7, p8);
            line7_8.Layer = contour;
            dxf.AddEntity(line7_8);

            var angle9 = 123.410638 * Pi / 180;
            var dis9 = 7.867485;
            var p9 = Vector2.Polar(origin, dis9, angle9);
            var line8_9 = new Line(p8, p9);
            line8_9.Layer = contour;
            dxf.AddEntity(line8_9);

            var angle10 = 123.614135 * Pi / 180;
            var dis10 = 7.856441;
            var p10 = Vector2.Polar(origin, dis10, angle10);
            var line9_10 = new Line(p9, p10);
            line9_10.Layer = contour;
            dxf.AddEntity(line9_10);

            //第三段弧的起点
            var angle11 = 123.625028 * Pi / 180;
            var dis11 = 7.855853;
            var p11 = Vector2.Polar(origin, dis11, angle11);
            var line10_11 = new Line(p10, p11);
            line10_11.Layer = contour;
            dxf.AddEntity(line10_11);
            //第三段弧终点
            var angle12 = 124.562786 * Pi / 180;
            var dis12 = 7.865355;
            var p12 = Vector2.Polar(origin, dis12, angle12);
            var arc3R = 0.118942;
            var arc3D = 65.680235 * Pi / 180;
            var arc3 = Pus.TwoPointArc(p11, p12, arc3R, false);
            arc3.Layer = contour;
            dxf.AddEntity(arc3);

            //第四段弧起点
            var angle13 = 124.565902 * Pi / 180;
            var dis13 = 7.864864;
            var p13 = Vector2.Polar(origin, dis13, angle13);
            var line12_13 = new Line(p12, p13);
            line12_13.Layer = contour;
            dxf.AddEntity(line12_13);
            //第四段弧终点
            var angle14 = 125.050980 * Pi / 180;
            var dis14 = 7.908750;
            var p14 = Vector2.Polar(origin, dis14, angle14);
            var arc4R = 0.410317;
            var arc4D = 11.175088 * Pi / 180;
            var arc4 = Pus.TwoPointArc(p13, p14, arc4R, false);
            arc4.Layer = contour;
            dxf.AddEntity(arc4);

            var angle15 = 125.495285 * Pi / 180;
            var dis15 = 7.946508;
            var p15 = Vector2.Polar(origin, dis15, angle15);
            var line14_15 = new Line(p14, p15);
            line14_15.Layer = contour;
            dxf.AddEntity(line14_15);

            //第五段弧起点
            var angle16 = 125.500035 * Pi / 180;
            var dis16 = 7.945445;
            var p16 = Vector2.Polar(origin, dis16, angle16);
            var line15_16 = new Line(p15, p16);
            line15_16.Layer = contour;
            dxf.AddEntity(line15_16);
            //第五段弧终点
            var angle17 = 127.023698 * Pi / 180;
            var dis17 = 7.888545;
            var p17 = Vector2.Polar(origin, dis17, angle17);
            var arc5R = 0.147498;
            var arc5 = Pus.TwoPointArc(p16, p17, arc5R, true);
            arc5.Layer = contour;
            dxf.AddEntity(arc5);

            //第六段弧起点
            var angle18 = -136.081476 * Pi / 180;
            var dis18 = 6.594298;
            var p18 = Vector2.Polar(origin, dis18, angle18);
            var line17_18 = new Line(p17, p18);
            line17_18.Layer = contour;
            dxf.AddEntity(line17_18);
            //第六段弧终点
            var angle19 = -134.960556 * Pi / 180;
            var dis19 = 6.691309;
            var p19 = Vector2.Polar(origin, dis19, angle19);
            var arc6R = 0.216443;
            var arc6 = Pus.TwoPointArc(p18, p19, arc6R, true);
            arc6.Layer = contour;
            dxf.AddEntity(arc6);

            var angle20 = -134.965109 * Pi / 180;
            var dis20 = 6.693253;
            var p20 = Vector2.Polar(origin, dis20, angle20);
            var line19_20 = new Line(p19, p20);
            line19_20.Layer = contour;
            dxf.AddEntity(line19_20);

            var angle21 = -125.557943 * Pi / 180;
            var dis21 = 7.106469;
            var p21 = Vector2.Polar(origin, dis21, angle21);

            var angle22 = -125.560517 * Pi / 180;
            var dis22 = 7.107163;
            var p22 = Vector2.Polar(origin, dis22, angle22);

            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
            Console.WriteLine("生成DXF文件" + path);
        }

        public static void DetailDesignThreadInsert(Dictionary<string, object> data)
        {
            var dxf = new DxfDocument();
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);

            string path = Convert.ToString(data["path"]);
            try
            {
                path = Convert.ToString(data["path"]);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            #region 变量
            var holeDia = 3.4;//通孔直径
            try
            {
                holeDia = Convert.ToDouble(data["holedia"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[holedia] not found");
            }

            var chamfer4HoleDia = 5.6;//通孔倒角直径
            try
            {
                chamfer4HoleDia = Convert.ToDouble(data["cfdia"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[cfdia] not found");
            }


            var icDia = 9.48;//内切圆直径
            try
            {
                icDia = Convert.ToDouble(data["ic"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[ic] not found");
            }
            var h = 15.4 / 9.48 * icDia;

            var pdptl = 1.2 / 9.48 * icDia;
            try
            {
                pdptl = Convert.ToDouble(data["pdptl"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdptl] not found");
            }

            var pdptr = 1.133 / 9.48 * icDia;
            try
            {
                pdptr = Convert.ToDouble(data["pdptr"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdptr] not found");
            }

            var pdx = 1.2 / 9.48 * icDia;
            try
            {
                pdx = Convert.ToDouble(data["pdx"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdx] not found");
            }

            var pdy = 0.93 / 9.48 * icDia;
            try
            {
                pdy = Convert.ToDouble(data["pdy"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[pdy] not found");
            }

            var cre = 0.25;
            try
            {
                cre = Convert.ToDouble(data["cre"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[cre] not found");
            }

            //齿左侧圆弧
            var retl = 0.25;
            try
            {
                retl = Convert.ToDouble(data["retl"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[retl] not found");
            }

            //齿右侧圆弧
            var retr = 0.25;
            try
            {
                retr = Convert.ToDouble(data["retr"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[retr] not found");
            }

            //左侧修光刃末端圆弧半径
            var rel = 0.0;
            try
            {
                rel = Convert.ToDouble(data["rel"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[rel] not found");
            }
            //右侧修光刃末端圆弧半径
            var rer = 0.0;
            try
            {
                rer = Convert.ToDouble(data["rer"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[rer] not found");
            }

            var par = 27.5 * Pi / 180;
            try
            {
                par = Convert.ToDouble(data["par"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[par] not found");
            }

            var pal = 27.5 * Pi / 180;
            try
            {
                pal = Convert.ToDouble(data["pal"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[pal] not found");
            }

            //右侧锥度
            var rightWiperAgl = 0 * Pi / 180;
            try
            {
                rightWiperAgl = Convert.ToDouble(data["ra"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[la] not found");
            }
            //左侧锥度
            var leftWiperAgl = 0 * Pi / 180;
            try
            {
                leftWiperAgl = Convert.ToDouble(data["la"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[ra] not found");
            }
            var r1 = 0.8;

            var gan = 15 * Pi / 180;
            try
            {
                gan = Convert.ToDouble(data["gan"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[gan] not found");
            }

            var alpha2 = 45 * Pi / 180;
            //刀尖厚度
            var s = 3.55;
            try
            {
                s = Convert.ToDouble(data["s"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[s] not found");
            }
            //中间厚度
            var thickness = 3.65;
            try
            {
                thickness = Convert.ToDouble(data["s1"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[s1] not found");
            }
            //断屑岛底面三角形尖点与齿顶的垂直距离（前面测得）
            var it = 2.3;
            try
            {
                it = Convert.ToDouble(data["it"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[it] not found");
            }
            //断屑岛高度
            var ih = 0.2;
            try
            {
                ih = Convert.ToDouble(data["ih"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[ih] not found");
            }
            //断屑岛拔模角度
            var iagl = 30 * Pi / 180;
            try
            {
                iagl = Convert.ToDouble(data["iagl"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[iagl] not found");
            }
            //断屑岛过渡半径
            var icr = 0.2;
            try
            {
                icr = Convert.ToDouble(data["icr"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[icr] not found");
            }
            //钝圆半径
            var dr = 0.05;
            try
            {
                dr = Convert.ToDouble(data["dr"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[dr] not found");
            }
            //断屑槽宽度
            var lbb = 0.4;
            try
            {
                lbb = Convert.ToDouble(data["lbb"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[lbb] not found");
            }
            //断屑槽底部圆弧半径
            var rbb = 1.0;
            try
            {
                rbb = Convert.ToDouble(data["rbb"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[rbb] not found");
            }
            //断屑槽圆弧与断屑岛过渡圆弧半径
            var ibcr = 0.25;
            try
            {
                ibcr = Convert.ToDouble(data["ibcr"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[ibcr] not found");
            }
            var pbcr = 0.25;
            try
            {
                pbcr = Convert.ToDouble(data["pbcr"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[pbcr] not found");
            }
            //断屑槽底部圆弧高度
            var rbh = 2.5;
            try
            {
                rbh = Convert.ToDouble(data["rbh"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[rbh] not found");
            }

            var chamferCD = 60 * Pi / 180;
            try
            {
                chamferCD = Convert.ToDouble(data["cfagl"]) * Pi / 180;
            }
            catch (Exception e)
            {
                Console.WriteLine("[cfagl] not found");
            }
            #endregion

            var origin = new Vector2(0, 0);
            var holeC = new Circle(origin, holeDia / 2);
            holeC.Layer = contour;
            dxf.AddEntity(holeC);
            var chamferC = new Circle(origin, chamfer4HoleDia / 2);
            chamferC.Layer = contour;
            dxf.AddEntity(chamferC);
            var ic = new Circle(origin, icDia / 2);
            ic.Layer = center;
            dxf.AddEntity(ic);
            var le = icDia / Math.Tan(Pi / 6);//理论边长（从动），由内切圆尺寸驱动
            var hf = h - le / 2;
            //轮廓点
            var p1 = new Vector2(origin.X - icDia / 2, origin.Y);
            var triangleCor = new Vector2(origin.X - icDia / 2, origin.Y + le / 2);
            var profileTop = new Vector2(triangleCor.X + pdx, triangleCor.Y - pdy);
            //齿顶圆弧的圆心
            var profileCen = Vector2.Polar(profileTop, cre, -Pi / 2);
            //齿顶的理论尖点
            var p4 = Vector2.Polar(profileCen, cre / Math.Sin((pal + par) / 2), Pi / 2 + par - (par + pal) / 2);
            //理论尖点到p3的垂直距离
            var p3_p4_VerticalDis = pdptl - retl * Math.Tan((Pi / 2 - pal - leftWiperAgl) / 2) * Math.Sin(leftWiperAgl);
            var p3 = Vector2.Polar(p4, p3_p4_VerticalDis / Math.Cos(pal), -Pi / 2 - pal);
            var p2 = fun.Inters(p3, Vector2.Polar(p3, 1, -Pi + leftWiperAgl), p1, Vector2.Polar(p1, 1, Pi / 2));
            //理论尖点到p5的垂直距离
            var p4_p5_VerticalDis = pdptr + retr * Math.Tan((Pi / 2 - par + rightWiperAgl) / 2) * Math.Sin(rightWiperAgl);
            var p5 = Vector2.Polar(p4, p4_p5_VerticalDis / Math.Cos(par), -Pi / 2 + par);
            var p7 = Vector2.Polar(origin, icDia / 2, Pi / 3);
            var p6 = fun.Inters(p5, Vector2.Polar(p5, 1, rightWiperAgl), p7, Vector2.Polar(p7, 1, Pi - 30 * Pi / 180));

            var contour0 = Pus.AddPointsToLwPolyLine(p1, p2, p3, p4, p5, p6, p7);
            contour0 = Pus.Fillet4LwPline(contour0, 1, rel);
            contour0 = Pus.Fillet4LwPline(contour0, 3, retl);
            contour0 = Pus.Fillet4LwPline(contour0, 5, cre);
            contour0 = Pus.Fillet4LwPline(contour0, 7, retr);
            contour0 = Pus.Fillet4LwPline(contour0, 9, rer);
            contour0.Layer = contour;
            dxf.AddEntity(contour0);

            var contour1 = Pus.RotateByOrigin(Pi * 2 / 3, contour0);
            dxf.AddEntity(contour1);
            var contour2 = Pus.RotateByOrigin(-Pi * 2 / 3, contour0);
            dxf.AddEntity(contour2);

            var scalingRatio = 6;
            var sm2 = new Matrix2(scalingRatio, 0,
                                                0, scalingRatio);
            var contourS = Pus.ScalingByMatrix2(contour0, sm2);
            dxf.AddEntity(contourS);

            //fu断屑槽
            var cbc = new Circle(profileTop, lbb);
            var arcS = Pus.LineXCir(cbc, p1, p2)[0];
            var arcE = Pus.LineXCir(cbc, p6, p7)[1];
            var arcBulge = Math.Tan(Vector2.Angle(arcE - profileTop) / 4 - Vector2.Angle(arcS - profileTop) / 4);
            var arc = Pus.AddPointsToLwPolyLine(arcS, arcE);
            arc.Vertexes[0].Bulge = arcBulge;
            arc.Layer = contour;
            dxf.AddEntity(arc);
            dxf.AddEntity(Pus.RotateByOrigin(-2 * Pi / 3, arc));
            dxf.AddEntity(Pus.RotateByOrigin(2 * Pi / 3, arc));
            var profileText = new Text("轮廓放大比例" + scalingRatio + ":1", sm2 * p4 + new Vector2(0, 10), 3, 0);
            dxf.AddEntity(profileText);

            var vs = new List<Vector2>();
            foreach (var vertex in contour0.Vertexes)
            {
                vs.Add(vertex.Position);
            }

            //pou视图
            var c0 = new Vector2(vs[0].X - le, vs[0].Y);
            var c1 = new Vector2(c0.X, c0.Y + holeDia / 2);
            var c2 = new Vector2(c1.X, profileCen.Y + cre);
            var c3 = new Vector2(c2.X - s, c2.Y);

            //basisPoint是俯视图下的点
            var basisPoint = fun.Inters(contour0.Vertexes[3].Position, contour0.Vertexes[8].Position, p4,
                Vector2.Polar(p4, 1, Pi / 2));
            //basisPointPou是pou视图下的点
            var basisPointPou = fun.Inters(basisPoint, Vector2.Polar(basisPoint, 1, Pi), c3,
                Vector2.Polar(c3, 1, Pi / 2 + gan));

            var c4 = Vector2.Polar(basisPointPou, it, -Pi / 2 + gan);
            var c5 = Vector2.Polar(c4, ih / Math.Sin(iagl), -Pi / 2 + gan - iagl);
            var c6 = Vector2.Polar(c5, 1, -Pi / 2 + gan);

            var cx = new Vector2(c0.X - thickness, profileTop.Y - lbb);
            var rbCen = new Vector2(cx.X - (rbh - thickness),
                cx.Y + Math.Sqrt(Math.Pow(rbb, 2) - Math.Pow((rbh - thickness), 2)));
            var rbc = new Circle(rbCen, rbb);
            var cy = Pus.LineXCir(rbc, c5, c6)[1];

            var c7 = new Vector2(c0.X - thickness, c0.Y + chamfer4HoleDia / 2);
            var c13 = Vector2.Polar(c7, (chamfer4HoleDia - holeDia) / 2 / Math.Sin(chamferCD / 2), -chamferCD / 2);

            //断屑岛与断屑槽过渡圆弧
            //            var k = (c5.X - cy.X) / (c5.Y - cy.Y);//c5,cy是目标直线上的两点，k是直线斜率
            //            var m = rbCen.X;//目标圆x坐标
            //            var n = rbCen.Y;//目标圆y坐标
            //            var a = k * (rbb - ibcr);
            //            var b = rbb - ibcr;
            //            var c = k * n + k * ibcr * Math.Sin(gan) - k * cy.Y - m - ibcr * Math.Cos(gan) + cy.X;
            //            var theAgl = Pus.GetFormatAlpha(a, b, c);
            //            var arc1Cen = Vector2.Polar(rbCen, rbb - ibcr, -theAgl);
            var arc1Cen = Pus.GetTangentCircleCenter(new Circle(rbCen, rbb), c5, cy, ibcr);
            var arc1S = Vector2.Polar(rbCen, rbb, Vector2.Angle(rbCen, arc1Cen));
            var arc1E = Vector2.Polar(arc1Cen, ibcr, gan);
            var arc1 = Pus.TwoPointArc(arc1S, arc1E, ibcr, true);
            //断屑槽与凸台过渡圆弧
            var beta = Math.Asin((c7.X - rbCen.X + pbcr) / (rbb + pbcr));
            var arc2Cen = Vector2.Polar(rbCen, rbb + pbcr, -Pi / 2 + beta);
            var arc2S = Vector2.Polar(arc2Cen, pbcr, Pi / 2 + beta);
            var arc2E = Vector2.Polar(arc2Cen, pbcr, Pi);
            var arc2 = Pus.TwoPointArc(arc2S, arc2E, pbcr, true);

            var rbArc = Pus.TwoPointArc(arc2S, arc1S, rbb, true);
            rbArc.Layer = contour;

            //将pou视图的点放到一个lwpolyline对象中
            var pline = Pus.AddPointsToLwPolyLine(c0, c1, c2, c3, c4, c5, arc1E);
            pline.Layer = contour;
            pline = Pus.Fillet4LwPline(pline, 3, dr);
            pline = Pus.Fillet4LwPline(pline, 5, icr);
            pline = Pus.Fillet4LwPline(pline, 7, icr);
            pline.Vertexes[9].Bulge = -Pus.GetArcBulge(Pus.ToV2(arc1.Center), arc1S, arc1E);
            pline.Vertexes.Add(new LwPolylineVertex(arc1S, -Pus.GetArcBulge(Pus.ToV2(rbArc.Center), arc2S, arc1S)));
            pline.Vertexes.Add(new LwPolylineVertex(arc2S, Pus.GetArcBulge(Pus.ToV2(arc2.Center), arc2S, arc2E)));
            pline.Vertexes.Add(new LwPolylineVertex(arc2E));
            pline = Pus.AddPointsToLwPolyLine(pline, c7, c13, c1);

            //对pou视图进行缩放
            var pouScaler = 6;
            var pouSMatrix2 = new Matrix2(pouScaler, 0,
                                        0, pouScaler);
            var plinescal = Pus.ScalingByMatrix2(pline, pouSMatrix2);
            var psOffset = new Vector2(le * 5, 0);
            plinescal = Pus.OffsetLwPolyline(plinescal, psOffset);
            dxf.AddEntity(plinescal);
            var psvs = new List<Vector2>();
            foreach (var vertex in plinescal.Vertexes)
            {
                psvs.Add(vertex.Position);
            }



            // var testLine = Pus.AddPointsToLwPolyLine(new Vector2(1, 1), new Vector2(1.05, 1.05));
            // testLine.Vertexes[0].Bulge = 1;
            // dxf.AddEntity(testLine);
            //标注
            #region dim
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;

            dimensionStyle.TextOffset = 0.1;
            dimensionStyle.TextHeight = 0.2;
            dimensionStyle.TextStyle.Height = 0.5;

            double offset = 2;

            //内切圆ic标注
            var dim_ic = new DiametricDimension(ic, -45, 10, dimensionStyle);
            dim_ic.UserText = "IC = " + Math.Round(icDia, 4);
            dxf.AddEntity(dim_ic);
            //断屑岛与断屑槽底部圆弧过渡圆弧标注
            var arc1Dim = Pus.ReDimension(plinescal, 4, dimensionStyle,
                Pus.GetArcCenterLineAglD(arc1) + 180, offset * 2);
            arc1Dim.UserText = "" + Math.Round(arc1.Radius, 4);
            dxf.AddEntity(arc1Dim);
            //断屑槽圆弧标注
            var rbArcDim = Pus.ReDimension(plinescal, 5, dimensionStyle,
                Pus.GetArcCenterLineAglD(rbArc), offset);
            rbArcDim.UserText = "" + Math.Round(rbArc.Radius, 4);
            dxf.AddEntity(rbArcDim);
            //断屑槽底部圆弧与凸台过渡圆弧标注
            var arc2Dim = Pus.ReDimension(plinescal, 6, dimensionStyle,
                Pus.GetArcCenterLineAglD(arc2), offset * 2);
            arc2Dim.UserText = "" + Math.Round(arc2.Radius, 4);
            dxf.AddEntity(arc2Dim);
            //断屑槽底部圆弧圆心高度
            var rbhDim = new LinearDimension(Pus.ToV2(Pus.GetArcFromPline(plinescal, 5).Center), psvs[1], offset, 0, dimensionStyle);
            rbhDim.UserText = rbh + "";
            dxf.AddEntity(rbhDim);
            //刀尖钝圆标注
            var drDim1 = Pus.ReDimension(plinescal, 1, dimensionStyle, Pus.GetArcCenterLineAglD(Pus.GetArcFromPline(plinescal, 1)), offset);
            drDim1.UserText = "dr=" + dr;
            dxf.AddEntity(drDim1);
            //gan标注
            var ganAglCen = pouSMatrix2 * c3 + psOffset;
            var ganAglSta = Vector2.Polar(ganAglCen, 1, Pi / 2);
            var ganaAglEnd = Vector2.Polar(ganAglCen, 1, Pi / 2 + gan);
            var ganDim = new Angular3PointDimension(ganAglCen, ganAglSta, ganaAglEnd, offset * 3, dimensionStyle);
            ganDim.UserText = "gan=" + Math.Round(gan * 180 / Pi, 4) + "°";
            dxf.AddEntity(ganDim);
            //断屑岛定位标注
            var itDim = new LinearDimension(pouSMatrix2 * basisPointPou + psOffset, pouSMatrix2 * c4 + psOffset, offset * 2, -90 + gan * 180 / Pi,
                dimensionStyle);
            itDim.UserText = it + "";
            dxf.AddEntity(itDim);
            //断屑岛过渡圆弧标注
            var icrDim1 = Pus.ReDimension(plinescal, 2, dimensionStyle, Pus.GetArcCenterLineAglD(Pus.GetArcFromPline(plinescal, 2)) + 180, offset * 2);
            icrDim1.UserText = "icr=" + icr;
            dxf.AddEntity(icrDim1);
            //断屑岛拔模角度标注
            Pus.NeedleDim(dxf, new Line(psvs[6], psvs[7]), new Line(psvs[6], Vector2.Polar(psvs[6], 1, -Pi / 2 + gan)),
                offset * 0.3,
                Math.Round(iagl * 180 / Pi) + "°", dimensionStyle);
            //断屑岛高度
            var dimStyle = dimensionStyle.Clone() as DimensionStyle;
            var ihDim = new LinearDimension(psvs[5], psvs[8], -3 * offset, gan * 180 / Pi, dimStyle);
            ihDim.UserText = ih + "";
            dxf.AddEntity(ihDim);
            //断屑岛过渡圆弧标注
            var icrDim2 = Pus.ReDimension(plinescal, 3, dimensionStyle, Pus.GetArcCenterLineAglD(Pus.GetArcFromPline(plinescal, 3)), offset * 2);
            icrDim2.UserText = "icr=" + icr;
            dxf.AddEntity(icrDim2);
            //断屑槽宽度标注（凸台与断屑槽底部圆弧未过渡时的交点 与 齿轮廓最高点所在直线 的距离）
            var lbbDim = new LinearDimension(pouSMatrix2 * cx + psOffset, pouSMatrix2 * c3 + psOffset, offset * 3, 90, dimensionStyle);
            lbbDim.UserText = "lbb=" + lbb;
            dxf.AddEntity(lbbDim);
            //刀片总厚度
            var s1Dim = new LinearDimension(psvs[1], psvs[13], offset, 0, dimensionStyle);
            s1Dim.UserText = "s1=" + thickness;
            dxf.AddEntity(s1Dim);
            //刀尖未倒钝圆时高度
            var sDim = new LinearDimension(psvs[2], pouSMatrix2 * c3 + psOffset, offset, 0, dimensionStyle);
            sDim.UserText = "s=" + s;
            dxf.AddEntity(sDim);

            //俯视图轮廓标注
            var leftrDim = Pus.ReDimension(contourS, 1, dimensionStyle, 150, offset);
            leftrDim.UserText = rel + "";
            dxf.AddEntity(leftrDim);

            var retlDim = Pus.ReDimension(contourS, 2, dimensionStyle, 135, offset);
            retlDim.UserText = "retl=" + retl + "";
            dxf.AddEntity(retlDim);

            var creDim = Pus.ReDimension(contourS, 3, dimensionStyle, 80, offset);
            creDim.UserText = "cre=" + cre + "";
            dxf.AddEntity(creDim);

            var retrDim = Pus.ReDimension(contourS, 4, dimensionStyle, 45, offset);
            retrDim.UserText = "retr=" + retr + "";
            dxf.AddEntity(retrDim);

            var rightrDim = Pus.ReDimension(contourS, 5, dimensionStyle, 60, offset);
            rightrDim.UserText = rer + "";
            dxf.AddEntity(rightrDim);

            var verticalLine4PalPar = new Line(sm2 * p4, sm2 * Vector2.Polar(p4, 1, -Pi / 2));
            verticalLine4PalPar.Layer = center;
            dxf.AddEntity(verticalLine4PalPar);

            var palDim = new Angular2LineDimension(new Line(sm2 * p4, sm2 * p3), verticalLine4PalPar, offset * 5, dimensionStyle);
            palDim.UserText = "pal=" + Math.Round(pal * 180 / Pi, 4) + "°";
            dxf.AddEntity(palDim);

            var parDim = new Angular2LineDimension(new Line(sm2 * p4, sm2 * p5), verticalLine4PalPar, offset * 3, dimensionStyle);
            parDim.UserText = "par=" + Math.Round(par * 180 / Pi, 4) + "°";
            dxf.AddEntity(parDim);
            //轮廓左手圆弧与左手修光刃切点到轮廓最高点距离
            var pdptlDim = new LinearDimension(sm2 * vs[2], sm2 * profileTop, offset * 4, 90, dimensionStyle);
            pdptlDim.UserText = "pdptl=" + pdptl;
            dxf.AddEntity(pdptlDim);
            //轮廓右手圆弧与右手修光刃切点到轮廓最高点距离
            var pdptrDim = new LinearDimension(sm2 * vs[8], sm2 * profileTop, offset * 6, -90, dimensionStyle);
            pdptrDim.UserText = "pdptr=" + pdptr;
            dxf.AddEntity(pdptrDim);

            var pdxDim = new LinearDimension(sm2 * triangleCor, sm2 * profileTop, offset * 2, 0, dimensionStyle);
            pdxDim.UserText = "pdx=" + pdx;
            dxf.AddEntity(pdxDim);

            var pdyDim = new LinearDimension(sm2 * triangleCor, sm2 * profileTop, offset * 3, 90, dimensionStyle);
            pdyDim.UserText = "pdy=" + pdy;
            dxf.AddEntity(pdyDim);
            //左侧修光刃倾角
            Pus.NeedleDim(dxf, new Line(sm2 * vs[2], sm2 * Vector2.Polar(vs[2], 1, 0)),
                new Line(sm2 * vs[2], sm2 * vs[3]),
                offset * 0.7, Math.Round(leftWiperAgl * 180 / Pi, 4) + "°", dimensionStyle);
            //右侧修光刃倾角
            Pus.NeedleDim(dxf, new Line(sm2 * vs[8], sm2 * Vector2.Polar(vs[8], 1, 0)),
                new Line(sm2 * vs[8], sm2 * vs[9]),
                offset, Math.Round(rightWiperAgl * 180 / Pi, 4) + "°", dimensionStyle);
            #endregion

            //图框
            var isframe = 0;
            try
            {
                isframe = Convert.ToInt16(data["isframe"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[isframe] not found");
            }
            if (isframe != 0)
            {
                var frameInfo = new Dictionary<string, object>();
                frameInfo.Add("比例", "1:1");
                Frame.frameA4Y(dxf, new Vector2(c0.X - Frame.A4Y.X / 2, c0.Y + Frame.A4Y.Y / 2), frameInfo);
            }


            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
            Console.WriteLine("生成DXF文件" + path);
        }
    }
}